package com.gcxy.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @Author lichuandong
 * @Date 2022/2/16
 */
@Component
public class RedisTemplateUtil {

    @Autowired
    private RedisTemplate redisTemplate;


    public String getString(String key) {
        Object o = redisTemplate.opsForValue().get(key);
        return (String) o;
    }

    public void setString(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    public List<String> getList(String key) {
        List<String> range = redisTemplate.opsForList().range(key, 0, -1);
        return range;
    }

    public void setList(String key, List<String> list) {
        redisTemplate.opsForList().leftPushAll(key, list);

    }

    public void remove(String key) {
        redisTemplate.delete(key);
    }

    public void setHourExpire(String key, Integer time) {
        redisTemplate.expire(key, time, TimeUnit.HOURS);
    }

    public void setMinuteExpire(String key, Integer time) {
        redisTemplate.expire(key, time, TimeUnit.MINUTES);
    }
}
